<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>chat</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        body {
            padding: 4.5rem;
            margin: 0;
            background: #edc0bf;
            background: linear-gradient(90deg, #edc0bf 0,#c4caef 58%);
            font-family: 'Inter', sans-serif;
        }
        .chat-box {
            margin: 40px auto;
            width: 50vw;
            background: rgba(255, 255, 255, .6);
            -webkit-backdrop-filter: blur(10px);
            backdrop-filter: blur(10px);
            box-shadow: 5px 5px 20px rgba(0, 0, 0, .3);
            padding: 24px;
            border-radius: 16px;
        }
        .oper {
            display: flex;
            margin-top: 16px;
        }
        #problem {
            outline: none;
            border: none;
            resize:none;
            flex: 1;
            font-size: 14px;
            padding: 8px;
            border-radius: 8px 0 0 8px;
        }
        #send {
            width: 100px;
            border: none;
            padding: 8px;
            background-color: rgb(202,200,231);
            color: #fff;
            font-size: 18px;
            cursor: pointer;
            border-radius: 0 8px 8px 0;
            transition: 0.3s;
        }
        #send:active {
            background-color: #DBC4D3;
        }
        #message-box {
            height: 50vh;
            margin: 16px 0;
            background-color: rgba(255, 255, 255, .7);
            border-radius: 8px;
            padding: 8px;
            overflow-y: scroll;
            scroll-behavior: smooth;
            line-height: 1.5;
            font-size: 14px;
        }
    </style>
</head>
<body>

    <div class="chat-box">
        <h1>chat</h1>
        <p class="oper">
            <textarea id="problem" >今天疯狂星期四吃什么？</textarea>
            <button id="send">发送</button>
        </p>
        <div id="message-box">
        </div>
    </div>
    <script>
        const msgBox = document.querySelector("#message-box");
        function createChatDom() {
            let id = "chat" + new Date().getTime();
            msgBox.innerHTML += `<p id="${id}">Chat：<p>`;
            return document.querySelector(`#${id}`);
        }
        function updateChatDom(chatDom, value) {
            chatDom.innerHTML += value;
        }

        function addProblem() {
            msgBox.innerHTML += `<p>Problem：${problem.value}<p>`;
        }

        function toViewBottom() {
            msgBox.scrollTo({
                top: msgBox.scrollHeight,
                behavior: "smooth" // 定义缓动动画
            });
        }
        send.onclick = async () => {
            if (problem.value.trim() === "") return;
            addProblem();
            const response = await fetch("/chat_stream", {
                method: "post",
                headers: {
                    "Content-Type": "application/json",
                    // 'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: JSON.stringify({
                    text: problem.value
                })
            });
            problem.value = "";
            const reader = response.body.getReader();
            const textDecoder = new TextDecoder();
            let chatDom = createChatDom();
            while (1) {
                const { done, value } = await reader.read();
                if (done) {
                    break;
                }
                updateChatDom(chatDom, textDecoder.decode(value));
                toViewBottom();
            }
        }
    </script>
</body>
</html>